//-----------------MATRICES---------------
float4x4 xViewProjection: WORLDVIEWPROJECTIONMATRIX;

//-----------------CONSTANTS----------------
float xBrightness = 0.7f;
		
float2 sobelMask[9] = {{1.0f,-1.0f}, {0.0f,-2.0f}, {-1.0f,-1.0f}, 
		{2.0f,0.0f}, {0.0f,0.0f}, {-2.0f,0.0f}, 
		{1.0f,1.0f}, {0.0f,-2.0f}, {-1.0f,1.0f}};
		
float3 col[9];    

//-----------------TEXTURE------------------
Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state { 
texture = <xColoredTexture>; 
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = Point; // LINEAR;
AddressU = mirror;
AddressV = mirror;};

//------------------- STRUCTS -------------------   
struct VertexToPixel
{
    float4 Position     : POSITION;
    float2 TexCoords    : TEXCOORD0;

};

struct PixelToFrame
{
    float4 Color : COLOR0;
};

//---------------------- SHADERS ------------------------

VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0)
{
    VertexToPixel Output = (VertexToPixel)0;
    Output.Position = mul(inPos, xViewProjection);
    Output.TexCoords = inTexCoords;

    return Output;    
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
    PixelToFrame Output = (PixelToFrame)0;
	
	for(int i=0; i < 9; i++){
	  col[i] = tex2D(ColoredTextureSampler, PSIn.TexCoords.xy + sobelMask[i]);
    }
	
	float x = col[2].x+col[8].x+col[5].x+col[0].x+col[3].x+col[6].x;
    float y = col[6].y+col[7].y+col[8].y+col[0].y+col[1].y+col[2].y;
	
	float gradient = col[5] * sqrt((x*x) + (y*y)) * xBrightness;
	Output.Color = gradient;
    return Output;
}


// ------------ TECHNIQUES --------------
technique Simplest{
    pass Pass0{        
        VertexShader = compile vs_3_0 SimplestVertexShader();
        PixelShader = compile ps_3_0 OurFirstPixelShader();
    }
}



